<?php
// 确保 app 文件夹存在
$appDir = __DIR__ . '/app';
if (!is_dir($appDir)) {
    mkdir($appDir, 0777, true);
}

// 示例 JSON 文件
$sampleFile = $appDir . '/example.json';
if (!file_exists($sampleFile)) {
    $sampleData = [
        "name" => "示例APP",
        "version" => "1.0.0",
        "description" => "这是一个示例应用程序。",
        "has_doc" => true,
        "doc_url" => "https://example.com/doc",
        "is_downloadable" => true,
        "download_url" => "https://example.com/download",
        "not_downloadable_message" => "此应用程序暂不可用。"
    ];
    file_put_contents($sampleFile, json_encode($sampleData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}

// 遍历 app 文件夹中的 JSON 文件
$apps = [];
foreach (glob($appDir . '/*.json') as $file) {
    $content = file_get_contents($file);
    $data = json_decode($content, true);
    if ($data) {
        $apps[] = $data;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>软件下载中心</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        header {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            text-align: left;
        }
        .container {
            padding: 20px;
        }
        .app-card {
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 15px;
        }
        .app-card h3 {
            margin: 0;
        }
        .app-card p {
            margin: 5px 0;
        }
        .btn {
            display: inline-block;
            padding: 10px 15px;
            background: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 5px;
        }
        .btn.disabled {
            background: #bbb;
            pointer-events: none;
        }
    </style>
</head>
<body>

<header>
    <h1>下载中心</h1>
</header>

<div class="container">
    <?php if (empty($apps)): ?>
        <p>暂无可用的应用程序，请添加新的 JSON 文件到 app 文件夹。</p>
    <?php else: ?>
        <?php foreach ($apps as $app): ?>
            <div class="app-card">
                <h3><?php echo htmlspecialchars($app['name']); ?> (版本：<?php echo htmlspecialchars($app['version']); ?>)</h3>
                <p><?php echo htmlspecialchars($app['description']); ?></p>
                <?php if (!empty($app['has_doc']) && !empty($app['doc_url'])): ?>
                    <p><a class="btn" href="<?php echo htmlspecialchars($app['doc_url']); ?>" target="_blank">查看文档</a></p>
                <?php endif; ?>
                <?php if (!empty($app['is_downloadable'])): ?>
                    <p><a class="btn" href="<?php echo htmlspecialchars($app['download_url']); ?>" target="_blank">下载</a></p>
                <?php else: ?>
                    <p class="btn disabled"><?php echo htmlspecialchars($app['not_downloadable_message']); ?></p>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

</body>
</html>